<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/19
 * Time: 0:42
 */

namespace app\admin\controller;


use app\admin\services\MenuService;
use app\admin\services\RoleService;
use app\common\controller\Base;
use app\common\model\RoleMenu;
use think\Exception;

class Role extends Base
{
    /**
     * @var RoleService
     */
    protected $_service;

    protected function _setService()
    {
        $this->_service = new RoleService();
    }

    public function savePower()
    {
        $roleId = I('post.roleId', 0, 'intval');
        $menuIds = I('post.menuIds', '', 'trim');
        if ($roleId) {
            $this->_service->savePower($roleId, $menuIds);
            $this->jsonSuccess();
        }
        $this->jsonFail();
    }


    public function form()
    {
        $menuService = new MenuService();
        $id = I('get.id', 0, 'intval');
        $tokens = [];
        if ($id) {
            $tokens = [];
            $tokenRows = RoleMenu::instance()->where(['role_id' => $id])->field('distinct(menu_token) menu_token')->select();
            foreach ($tokenRows as $row) {
                $tokens[] = $row['menu_token'];
            }
        }
        $menuList = $menuService->getMenuList(false);
        $this->assign(['menuList' => $menuList, 'tokens' => $tokens]);
        return parent::form(); // TODO: Change the autogenerated stub
    }
}